
cap program drop backlog_construction
program backlog_construction
*********************** Updated on 11 May 22; Created on 9 May 22 *************************************************
* This program needs the following 5 variables to be exactly the same name in the data: 
	*1) firm_id; 
	*2) bid_date; 
	*3) winbid; the amount the winning bidder receives. 
		* Specify a variable name as the FIRST argument if you use a customized variable name (added on 11may22);
	*4) winner; assigns 1 for the winner and 0 for others. 
		* Specify a variable name as the SECOND argument if you use a customized variable name (added on 11may22);
	*5) contract_id; numeric
*Note*
*1 Exceptions are 3) and 4). The code accepts a customized varname by specifing the name in the argument
* 
*2 FLAG; the codes ignores observations with FLAG == 1
********************************************************************************************

args arg_winbid arg_winner
global days "30 60 90 120 180 365"

local winbid = "winbid"
if "`arg_winbid'" != ""{
	local winbid = "`arg_winbid'"
}
local winner = "winner"
if "`arg_winner'" != ""{
    local winner = "`arg_winner'"
}



foreach v in "firm_id" "`winbid'" "bid_date" "`winner'" "contract_id"{
	*cap noisily di "`v'"
	cap sum `v' 
	if _rc != 0{
		di as error "`v' is missing" _continue
		di as text""
	}
}
cap gen FLAG = 0

/* create blogx capx utilx*/
foreach x in $days {
	set seed 1281283
	cap drop blog`x'
	gen double blog_temp = `winner' * (1-FLAG) * `winbid'
	gen double cum_temp = `winner' * (1-FLAG) 
	replace blog_temp = 0 if blog_temp==. 
	replace cum_temp = 0 if cum_temp==. 
	
	expand 2 if `winner' == 1 & FLAG == 0, gen(expand)
	replace blog_temp = -blog_temp if expand == 1 
	replace cum_temp = -cum_temp if expand == 1 
	replace bid_date = bid_date + `x' if expand == 1
	*replace contract_id = - contract_id if expand == 1
	*ソートしたときexpand=1がその日の最初にくるようにする。そうでないと、オークションによっては1年前の案件の償却が反映されない。
	gsort firm_id bid_date -expand

	by firm_id: replace blog_temp = blog_temp + blog_temp[_n-1] ///
		if firm_id == firm_id[_n-1]
	
	by firm_id: replace cum_temp = cum_temp + cum_temp[_n-1] ///
		if firm_id == firm_id[_n-1]
	
	
	replace blog_temp = blog_temp - `winbid' if `winner' == 1 /* 受注時のamountはblog_tempに含めない*/
	replace cum_temp = cum_temp - 1 if `winner' == 1 
	
	drop if expand == 1
	cap drop temp
	
	*Drop obs. with blog_tempclose to 0 (double)
	sum blog*, d
	*replace blog_temp = 0 if blog_temp < 1 & blog_temp !=.

	by firm_id bid_date: egen blog_x = min(blog_temp)
	replace blog_temp = blog_x
	by firm_id bid_date: egen cum_win_x = min(cum_temp)
	replace cum_temp = cum_win_x

	drop blog_x cum_win_x expand
	*もし同日に複数回オークションに勝ったら、オークションによっては同日に勝った別のオークションがバックログに入ってしまう。
	*するとソートのされ方によってUtilが変わる。
	*同日に勝ったオークションはバックログに入れない。
	rename blog_temp blog`x'
	rename cum_temp cum_win`x'
	replace blog`x' = 0 if blog`x' < .000001 & blog`x' > 0   /* added on 22jan22*/
	replace blog`x' =. if bid_date ==.    /* added on 22jan22*/ 
	replace cum_win`x' = 0 if cum_win`x' < .000001 & cum_win`x' > 0   /* added on 22jan22*/
	replace cum_win`x' =. if bid_date ==.    /* added on 22jan22*/ 
	
}
order firm_id `winner' `winbid' blog*

/* create date from winning */	
 sort firm_id bid_date
 * bidder毎に時系列でソートする
 by firm_id: gen ord=_n
 * bidder毎に順番をつける
 gen windate=bid_date if `winner'==1
 * 勝った日をつける
 replace windate=windate[_n-1] if ord>1 & `winner'~=1
 * 勝たなかった日に関しては最後に勝った日付をいれる
 gen date_from_win=bid_date-windate if `winner'~=1
 * 勝たなかった日に関しては最後に勝った日からの経過日数を入れる
 replace date_from_win=bid_date-windate[_n-1] if ord>1 & `winner'==1
 * 勝った日に関しても前回に勝った日からの経過日数を入れる。
 gen dates_after_win=1 if date_from_win~=.
 * 同日に複数回参加したオークションでは、過去に勝ったオークションからの日数に経過日数をそろえる
 by firm_id bid_date: ///
			egen dates_after_win_x = max(dates_after_win)
 drop dates_after_win
 rename dates_after_win_x dates_after_win

/* create capacity : max(blog365) in fy 2001 for each firm */
	/*bys bidder_muni_id: egen basefy = min(fy)
	bysort bidder_muni_id fy: egen ccc = max(blog365) if fy == basefy & fy < .
	bysort bidder_muni_id: egen cap = max(ccc)
	drop ccc*/
	bysort firm_id: egen cap = max(blog365)
	
/* sales / probiwin in the first fiscal year
	gen `winner'01 = 0
	replace `winner'01 = 1 if `winner' == 1
	bysort firm_id fy: egen rev1y = ///
		total( `winner' * (1-FLAG) * win_bid_pct * reserve_n )
	cap drop probwin1y
	bysort FLAG bidder_muni_id fy: egen probwin1y =	mean( winner01 ) ///
		if FLAG == 0
	drop winner01
	sort bidder_muni_id bid_date
	/* fy1y is the first fiscal year in which the firm bids */
	gen fy1y = fy
	replace fy1y = fy1y[_n-1] if bidder_muni_id == bidder_muni_id[_n-1] ///
		& _n > 1
	sort bidder_muni_id bid_date		
	replace rev1y = rev1y[_n-1] if bidder_muni_id == bidder_muni_id[_n-1] ///
		& _n > 1
	replace probwin1y = probwin1y[_n-1] if bidder_muni_id == bidder_muni_id[_n-1] ///
		& _n > 1
	br bidder_muni_id bidder fy bid_date rev1y*/

	
/* create util: blogx / cap and 2003 or later of fiscal year */
foreach x in $days {
	gen util`x' = blog`x' / cap /*if fy > basefy*/
}

/* create utilav: blogx / average backlog size in data period*/
foreach x in $days {
	bys firm_id: egen avrblog`x' = mean(blog`x')
	qui gen utilav`x' = blog`x' / avrblog`x'
}

/* create utilnm: (blogx - average backlog) / sd_backlog*/
foreach x in $days {
	cap drop avrblog`x'
	bys firm_id: egen avrblog`x' = mean(blog`x')
	bys firm_id: egen sdblog`x' = sd(blog`x')
	bys firm_id: egen maxblog`x' = max(blog`x')
	gen utilnm`x' = (blog`x' - avrblog`x')/sdblog`x'
	gen utilnm2_`x' = blog`x'/maxblog`x'
}

foreach x in $days {
	qui gen utildf`x' = blog`x' - avrblog`x'
}

/* create util_an: blogx / blog365*/
foreach x in $days {
	qui gen util_an`x' = blog`x' / blog365
}

/* log backlog */
foreach x in $days {
	cap drop lnblog`x'
	qui gen lnblog`x' = log(1 + blog`x')
	label var lnblog`x' "`x'-Days Log Backlog"
}
/*
/* log backlog */
foreach x in $days {
	cap drop lnblog`x'
	qui gen lnblog`x' = log(1 + blog`x')
	label var lnblog`x' "`x'-Days Log Backlog"
}
*/



*sort firm_code bid_win_date /* as of 14jan22 */
sort firm_id bid_date contract_id /* deadlineforbidsub contract_id added on 16jan22 to fix the order */
qui bysort firm_id bid_date contract_id : gen NN = _N
by firm_id: gen numa = _n if firm_id < .
/* constraint: "if firm_id < ." added on 16jan22*/

foreach x in $days {
	set seed 182273
	cap drop sumblog`x'
	by firm_id: gen sumblog`x' = sum(blog`x') if firm_id < .
	qui gen meanblog`x'=sumblog`x'/numa
	qui gen sqblog`x' = (blog`x')^2
	by firm_id: gen sumsqblog`x' = sum(sqblog`x')
	/*gen sdbacklog`x' = sqrt((sumsqblog`x' - (meanblog`x')^2)/(numa-1)) as of 20jan22*/
	qui gen sdbacklog`x' = sqrt((sumsqblog`x' - numa * (meanblog`x')^2)/(numa-1))
	qui gen rblog`x' = (blog`x' - meanblog`x')/sdbacklog`x' if firm_id < .
	*replace rblog`x' = . if blog`x' == 0 
	*replace rblog`x' = 0 if blog`x' == 0
	label var rblog`x' "`x'-Days Standardized Backlog"
}
drop NN numa 
compress
end
